﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
public partial class Admin_QuanLyQuanTriVien_EditUser : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    private Entities en = new Entities();
    private string strID = string.Empty;
    private string p = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
        {
            strID = Request.QueryString["id"].ToString();          
        }
        if (!IsPostBack)
        {
            this.GetQTinfo(strID);
        }
    }
    protected void GetQTinfo(string id)
    {
        SqlDataReader drd;
        SqlCommand comm = new SqlCommand("getQTbyID", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@ma", id);
        comm.Connection.Open();
        drd = comm.ExecuteReader();
        if (drd.Read())
        {
            this.txtma.Text = drd["ma"].ToString();
            this.txthoten.Text = drd["hoten"].ToString();
            this.txttaikhoan.Text = drd["taikhoan"].ToString();
            p = drd["matkhau"].ToString();
            this.txtdiachi.Text = drd["diachi"].ToString();
            this.txtdeinthoai.Text = drd["dienthoai"].ToString();
            ddl_quyen.SelectedIndex  = Convert.ToInt32(drd["quyen"].ToString());                                   
        }
        drd.Close();
        drd.Dispose();
        comm.Connection.Close();
        comm.Connection.Dispose();

    }
    protected void NV_Edit_Click(object sender, EventArgs e)
    {
        string username = txttaikhoan.Text;
        string pass = txtpass.Text;
        string uppass = "";
        if (pass.Equals(""))
        {
            uppass = p;
        }
        else {
            uppass = pass;
        }
        string hoten = txthoten.Text;
        string diachi = txtdiachi.Text;
        string dienthoai = txtdeinthoai.Text;
        int quyen = ddl_quyen.SelectedIndex;
        SqlCommand comm = new SqlCommand("updateQT", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@ma", strID);
        comm.Parameters.AddWithValue("@hoten", hoten);
        comm.Parameters.AddWithValue("@diachi", diachi);
        comm.Parameters.AddWithValue("@dienthoai", dienthoai);
        comm.Parameters.AddWithValue("@taikhoan", username);
        comm.Parameters.AddWithValue("@matkhau", en.EncodePassword(uppass,""));
        comm.Parameters.AddWithValue("@quyen", quyen);
        comm.Connection.Open();
        comm.ExecuteNonQuery();

        comm.Connection.Close();
        comm.Connection.Dispose();
        Response.Redirect("Danhsachquantri.aspx");
    }
}
